package com.useeinfo.oa.open.admin.wuHanCommon;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.action.BaseAction;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.SelectVO;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.wuHanCommon.base.biz.WuHanUnitBiz;
import com.useeinfo.oa.modules.wuHanCommon.base.entity.WuHanUnit;
import com.useeinfo.oa.modules.wuHanCommon.base.enums.MeterReadingEnum;
import com.useeinfo.oa.modules.wuHanCommon.base.enums.PointSystem;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/web/wuhan/unit")
public class WuHanUnitAction extends BaseAction {

    @Autowired
    private WuHanUnitBiz wuHanUnitBiz;

    /**
     * 打开列表页面
     */
    @RequestMapping("/getWuHanUnitListPage")
    public ModelAndView getWuHanUnitListPage() {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/wuHanCommon/unit/unitList");

        return modelAndView;
    }

    /**
     * 分页获取JSON数据
     */
    @RequestMapping("/getWuHanUnitListJSON")
    @ResponseBody
    public JSONObject getWuHanUnitListJSON(@RequestParam(value = "pageNo", required = false) String pageNowParam,
                                           @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                                           @RequestParam(value = "orderBy", required = false) String orderBy,
                                           @RequestParam(value = "searchCode", required = false) String searchCode) {

        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);
        queryParam.getSqlMap().put("searchCode", searchCode);
        if (StringUtils.isNotBlank(orderBy))
            queryParam.setSqlOrder("order by " + orderBy);
        return wuHanUnitBiz.findJSONList(queryParam);
    }

    /**
     * 获取编辑页面
     */
    @RequestMapping("/getWuHanUnitEditPage")
    public ModelAndView getWuHanUnitEditPage(@RequestParam(value = "id", required = false) String id) {


        WuHanUnit wuHanUnit = wuHanUnitBiz.findModel(StringConverters.ToLong(id));
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName("pages/wuHanCommon/unit/unitEditPart");
        modelAndView.addObject("wuHanUnit", wuHanUnit);
        return modelAndView;
    }

    /**
     * 执行提交的新增或修改请求
     */
    @RequestMapping("/executeWuHanUnitEdit")
    @ResponseBody
    public ResultDto executeWuHanUnitEdit(WuHanUnit wuHanUnit,
                                          @RequestParam(value = "select_automatic[]", required = false) List<String> automatics,
                                          @RequestParam(value = "select_system[]", required = false) List<String> systems) {
        if (CollectionUtils.isNotEmpty(automatics))
            wuHanUnit.setMeterReadingEnum(MeterReadingEnum.valueOf(automatics.get(0)));
        if (CollectionUtils.isNotEmpty(systems))
            wuHanUnit.setPointSystem(PointSystem.valueOf(systems.get(0)));
        if (wuHanUnit.getMeterReadingEnum() == null) {
            return ResultUtil.error("所属机组不能为空");
        }
        if (wuHanUnit.getPointSystem() == null) {
            return ResultUtil.error("所属系统不能为空");
        }
        if (wuHanUnitBiz.exist(wuHanUnit)) {
            return ResultUtil.error("位置为空或重复，请重新输入");
        }
        wuHanUnitBiz.addOrUpdate(wuHanUnit);
        return ResultUtil.success();
    }

    /**
     * 逻辑删除机构
     */
    @RequestMapping("/logicRemoveWuHanUnit")
    @ResponseBody
    public ResultDto logicRemoveWuHanUnit(@RequestParam(value = "id", required = false) String id) {
        wuHanUnitBiz.deleteLogic(wuHanUnitBiz.findModel(StringConverters.ToLong(id)));
        return ResultUtil.success();
    }

    /**
     * 获取设备选择数据
     *
     * @param id
     * @param q
     * @return
     */
    @RequestMapping("/getWuHanUnitSelect")
    @ResponseBody
    public Set<SelectVO> getWuHanUnitSelect(@RequestParam(value = "id", required = false) String id,
                                            @RequestParam(value = "q", required = false) String q) {
        Set<SelectVO> selectVOS = new HashSet<>();
        WuHanUnit wuHanUnit = wuHanUnitBiz.findModel(StringConverters.ToLong(id));
        if (wuHanUnit != null) {
            selectVOS.add(new SelectVO(wuHanUnit.getUnit(), wuHanUnit.getId() + ""));
        }
        QueryParam queryParam = new QueryParam(1, 20);
        queryParam.getSqlMap().put("magicLike", q);
        queryParam.getSqlMap().put("magicId", id);
        List<WuHanUnit> wuHanUnits = wuHanUnitBiz.findList(queryParam);
        wuHanUnits.forEach(unit -> selectVOS.add(new SelectVO(unit.getUnit(), unit.getId() + "")));
        return selectVOS;
    }

    /**
     * 根据类型获取设备选择数据
     *
     * @return
     */
    @RequestMapping("/getWuHanUnitSelectByMeterReading")
    @ResponseBody
    public ResultDto getWuHanUnitSelectByMeterReading(@RequestParam(value = "meterReading", required = false) String meterReading) {
        if (StringUtils.isBlank(meterReading)) {
            return ResultUtil.success();
        }
        JSONArray rows = new JSONArray();
        QueryParam queryParam = new QueryParam();
        if (StringUtils.isNotBlank(meterReading)) {
            String type = meterReading.split(",")[0];
            if ("0".equals(type)) {
                queryParam.getSqlMap().put("meterReading", meterReading.split(",")[1]);
            } else if ("1".equals(type)) {
                queryParam.getSqlMap().put("pointSystem", meterReading.split(",")[1]);
            }
        }
        List<WuHanUnit> wuHanUnits = wuHanUnitBiz.findList(queryParam);
        if (CollectionUtils.isNotEmpty(wuHanUnits)) {
            wuHanUnits.forEach(wuHanUnit -> rows.add(wuHanUnit.toJSONObject()));
        }
        return ResultUtil.success(rows);
    }
}
